<template>
  <div v-if="data.length === 0" class="loading">
    <p>数据加载中...</p>
  </div>
  <div v-else class="top">
    <div class="top-content">
      <RouterLink to="/" class="logo">
        <i class="iconfont icon-sun"></i>
        <h2>新中地天气</h2>
      </RouterLink>
      <h3>{{ city }}</h3>
      <span>实时天气：{{ data.weather }} {{ data.temperature }}℃</span>
      <span>{{ data.wind_direction }} {{ getWindPow(data.wind_power) }}</span>
      <div class="btn">
        <i class="iconfont icon-tip" @click="showTip = !showTip"></i>
        <i class="iconfont icon-add" v-if="canAddCity" @click="handleAddCity"></i>
      </div>
    </div>
    <!-- 提示模态框 -->
    <Transition>
      <div class="tip-modal" v-if="showTip">
        <div class="tip-content">
          <h4>关于:</h4>
          <p>这个应用可以用来追踪你选择城市的当前天气</p>
          <br />
          <h4>如何使用:</h4>
          <p>1.点击搜索框输入你希望追踪的城市</p>
          <p>2.在搜索结果中选中一个城市，你将获取当地最新的天气</p>
          <p>3.点击右侧的+号可以将追踪城市的天气情况保存在首页</p>
          <br />
          <h4>移除城市:</h4>
          <p>如果你不想在首页关注某个城市,可以通过底部的按钮删除它</p>
          <button @click="showTip = !showTip">关闭</button>
        </div>
      </div>
    </Transition>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue';
import { getAdcode, getWeather } from '@/api/index.js';
import { useWeatherStore } from '@/stores/weather.js';

const showTip = ref(false);

const weatherStore = useWeatherStore();

const canAddCity = computed(() => {
  return weatherStore.addCity;
});

const city = ref('');

const data = ref([]);

const getWindPow = (windPow) => {
  if (windPow === '微风') {
    return '≤3级';
  }
  if (windPow === '3-4级') {
    return '≤4级';
  }
  if (windPow === '4-7级') {
    return '≤7级';
  }
  if (windPow === '8-12级') {
    return '≤12级';
  }
  if (windPow === '13-18级') {
    return '>18级';
  }
  return '未知';
};

const handleAddCity = () => {
  weatherStore.setAddCity(false);
  weatherStore.setCityList();
};

onMounted(async () => {
  const res1 = await getAdcode();
  // console.log('ip定位', res1);
  city.value = res1.result.ad_info.city ? res1.result.ad_info.city : res1.result.ad_info.province;
  const adcode = res1.result.ad_info.adcode;
  const res2 = await getWeather(adcode);
  // console.log('天气服务', res2);
  const hour = new Date().getHours();
  if (hour >= 18 || hour < 6) {
    data.value = res2.result.forecast[0].infos[0].night;
  } else {
    data.value = res2.result.forecast[0].infos[0].day;
  }
  weatherStore.setData(res2.result.forecast[0].infos);
});
</script>

<style lang="scss" scoped>
@import 'https://at.alicdn.com/t/c/font_4989155_mni3tx2vjuq.css';
$bg-color-l: #00668a; //背景颜色-浅
$bg-color-d: #004e71; //背景颜色-深
$font-color-w: #fff; //字体颜色-白

.loading {
  padding-top: 20px;
  width: 100%;
  height: 80px;
  font-size: 20px;
  color: $font-color-w;
  text-align: center;
}

.top {
  display: flex;
  justify-content: center;

  width: 100%;
  height: 80px;
  box-shadow: 0 5px 10px rgba(0, 0, 0, 0.15);

  .top-content {
    display: flex;
    align-items: center;
    gap: 20px;

    position: relative;

    padding: 0 100px;

    width: 1024px;
    height: 100%;

    color: $font-color-w;
    .logo {
      display: flex;
      align-items: center;
      gap: 10px;
      text-decoration: none;
      color: $font-color-w;

      cursor: pointer;
    }
    .icon-sun {
      font-size: 24px;
    }
    .btn {
      position: absolute;
      top: 50%;
      right: 100px;
      transform: translateY(-50%);
      display: flex;
      align-items: center;
      gap: 10px;
    }
    .icon-tip {
      font-size: 20px;
      &:hover {
        color: $bg-color-d;
        cursor: pointer;
        transition: all 0.5s linear;
      }
    }
    .icon-add {
      font-size: 20px;
      &:hover {
        color: $bg-color-d;
        cursor: pointer;
        transition: all 0.2s linear;
      }
    }
  }

  .tip-modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    display: flex;
    justify-content: center;
    align-items: center;
    .tip-content {
      width: 340px;
      height: 320px;
      background-color: #fff;
      padding: 20px;
      h4 {
        font-weight: 400;
      }
      p {
        font-size: 12px;
      }
      button {
        background-color: $bg-color-l;
        color: $font-color-w;

        border: none;
        padding: 6px 20px;
        margin-top: 25px;
        font-size: 12px;
        cursor: pointer;
      }
    }
  }
}

//模态框的渐隐渐显动画
.v-enter-active,
.v-leave-active {
  transition: opacity 0.5s linear;
}

.v-enter-from,
.v-leave-to {
  opacity: 0;
}
</style>
